//标签类型，论文、著作...（0：论文 1：专利 2：项目 3：著作）
var category = 0;
var pageNumber = 1;
$(function() {
	labelGet(category);
	showThesis();
});

/**
 * 设置值
 * 
 * @param queryStr
 *            检索字段
 * @param type
 *            类型
 * @param lableId
 *            标签
 * @param pageNum
 *            页码
 */
function valueSet(queryStr, type, lableId, pageNum) {
	$("#queryStr").val(queryStr);
	$(".type").val(type);
	$("#lableId").val(lableId);
	$("#pageNo").val(pageNum);
}
function pageChange(pageNum) {
	pageNumber = pageNum;
//	pageNoId
	$("#pageNoId").val(pageNum);
	if (category == 1) {
		showPatent();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuanli-tables").show();
	} else if (category == 3) {
		showWork();
		$("#zhuanli-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuzuo-tables").show();
	} else if (category == 0) {
		showThesis();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").show();
	} else if (category == 2) {
		showProject();
		$("#zhuzuo-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").hide();
		$("#xiangmu-tables").show();
	}
}
/**
 * 当model改变，标签列表改变
 * 
 * @param value
 */
function modelChange(value) {
	pageNumber = 1;
	$("#pageNoId").val(pageNumber);
	labelGet(value);
	if (category.trim() == "1") {// 专利
		showPatent();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuanli-tables").show();
	} else if (category.trim() == "3") {// 著作
		showWork();
		$("#zhuanli-tables").hide();
		$("#xiangmu-tables").hide();
		$("#lunwen-tables").hide();
		$("#zhuzuo-tables").show();
	} else if (category.trim() == "0") {// 论文
		showThesis();
		$("#zhuzuo-tables").hide();
		$("#xiangmu-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").show();
	} else if (category.trim() == "2") {// 项目
		showProject();
		$("#zhuzuo-tables").hide();
		$("#zhuanli-tables").hide();
		$("#lunwen-tables").hide();
		$("#xiangmu-tables").show();
	}
	
}

/**
 * 显示标签
 * 
 * @param type
 */
function labelGet(type) {
	console.log("标签获取：" + type);
	category = type;
	$("#labelType").val(category);
	var content = "";
	$
			.post(
					"getLableList",
					$("#labelGetForm").serialize(),
					function(responseResult) {
						var list = responseResult.labelList;
						if (list.length > 0) {
							$(list)
									.each(
											function(i) {
												content += "<li><input type='checkbox' class='query' id='query"
														+ this.id
														+ "' name=''>"
														+ this.lable + "</li>";
											});
							$('#label').html(content);

							var count = responseResult.labelList.length;
							content = "";
							content += "<tr><td colspan='4'><strong style='font-size:20px;'>请选择标签:</strong></td></tr>";
							for (var i = 0; i < count / 4; i++) {
								content += "<tr>";
								for (var j = 4 * i; j < 4 * (i + 1); j++) {
									if (j < count) {
										content += "<td><div class='checkbox'><span class='guoxuan' style='font-size:20px' style=''>"+list[j].lable+"</span>" +
												"<input type='checkbox' value='1' class='dbq' id='dbq"+list[j].id+"'  name='' style='display:none;'>" +
												"<label for='dbq"+list[j].id+"' >√</label></div></td>";
									} else {
										content += "<td>&nbsp;</td>";
									}
								}
								content += "</tr>";
							}
							content += "<tr><td colspan='2'><input class='btn-download' type='button' "
									+ "value='确定' onclick='dbq()' style='width:100px'></td>"
									+ "<td colspan='2'><input class='btn-download' type='button' "
									+ "value='返回' onclick='returnbiaoqian();' style='width:100px'></td></tr>";
							$('#biaoqian').html(content);

						}
					});
}
/**
 * 打标签
 */
function dbq(){
	var labelIdStr = valueGetFromCheckbox2("dbq");
	var archivesIdStr = valueGetFromCheckbox();
	var dbqType = category;
	$("#dbqLabelIdStr").val(labelIdStr);
	$("#dbqarchivesIdStr").val(archivesIdStr);
	$("#dbqType").val(dbqType);
	//classifyArchives
	$.post("classifyArchives", $("#dbqForm").serialize(),
			function(responseResult) {
				alert(responseResult.responseParam.message);
				location.reload();
			});
	
}
function arcQuery() {
	var labelIdStr = valueGetFromCheckbox2("query");
	$("#lableIdStr").val(labelIdStr);
	$("#pageNoId").val(1);
	if (category == 0) {// 论文
		showThesis();
	} else if (category == 1) {// 专利
		showPatent();
	} else if (category == 2) {// 项目
		showProject();
	} else if (category == 3) {// 著作
		showWork();
	}

}
/**
 * 论文
 */
function showThesis() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		console.log("测试论文："+responseResult.archivesList.length);
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch'  />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#thesis').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#thesis').html("无相关数据！");
		}
	});
}

/**
 * 专利
 */
function showPatent() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		console.log("测试专利："+responseResult.archivesList.length);
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch'  />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#patent').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#patent').html("无相关数据！");
		}
	});
}
/**
 * 项目
 */
function showProject() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		console.log("测试项目："+responseResult.archivesList.length);
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch' />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#project').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#project').html("无相关数据！");
		}
	});
}

/**
 * 著作
 */
function showWork() {
	var content = "";
	$.post("getArchivesList", $("#queryForm").serialize(), function(
			responseResult) {
		console.log("测试著作："+responseResult.archivesList.length);
		if (responseResult.archivesList.length > 0) {
			$(responseResult.archivesList).each(
					function(i) {
						var statu = "";
						if (this.auditstatus == 0) {
							statu = "未审核";
						} else if (this.auditstatus == 1) {
							statu = "请求审核";
						} else if (this.auditstatus == 2) {
							statu = "审核失败";
						} else if (this.auditstatus == 3) {
							statu = "审核通过";
						}
						content += "<tr><td><input type='checkbox' id='"
								+ this.id + "' class='arch'  />" + "</td><td>"
								+ (i + 1) + "</td><td>" + this.title
								+ "</td><td>" + this.firstauthor + "</td><td>"
								+ this.department + "</td><td>" + statu
								+ "</a></td></tr>";
					});
			$('#work').html(content);
			pageShow(pageNumber, responseResult.responseParam.totalNum, '');
		} else {
			$('#work').html("无相关数据！");
		}
	});
}

function pageShow(curPage, totalPage, pageFlag) {
	var ta = "";
	var lastPage = curPage - 1, nextPage = curPage + 1;
	if (lastPage < 1) {
		lastPage = 1;
	}
	if (nextPage > totalPage) {
		nextPage = totalPage;
	}
	ta += "<li><a href='javascript:pageChange(" + lastPage
			+ ")'>&laquo;</a></li>";
	if (curPage > 2 && curPage < parseInt(parseInt(totalPage) - 2)) {
		for (var j = parseInt(parseInt(curPage) - 2); j <= totalPage
				&& j <= parseInt(parseInt(curPage) + 2); j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (curPage <= 2 && totalPage >= 5) {
		for (var j = 1; j <= 5; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else if (totalPage < 5) {
		for (var j = 1; j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	} else {
		for (var j = parseInt(parseInt(totalPage) - 4); j <= totalPage; j++) {
			if (j == curPage) {
				ta += "<li class='am-active'><a href='javascript:;'>" + j
						+ "</a></li>";
			} else {
				ta += "<li><a href='javascript:pageChange(" + j + ");'>" + j
						+ "</a></li>";
			}
		}
	}
	ta += "<li><a href='javascript:pageChange(" + nextPage
			+ ");'>&raquo;</a></li>";
	$("#pagebody").html(ta);
}
// 实现全选的js代码
function sellectAll() {
	var input = document.getElementsByTagName("input");
	var notSelectCount = 0;
	var inputCount = input.length;
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox" && input[i].className == "arch") {
			if (input[i].checked == false) {
				notSelectCount++;
			}
		}
	}
	if (notSelectCount == 0) {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox" && input[i].className == "arch") {
				input[i].checked = false;
			}
		}
	} else {
		for (var i = 0; i < inputCount; i++) {
			if (input[i].type == "checkbox" && input[i].className == "arch") {
				input[i].checked = true;
			}
		}
	}
}
/**
 * 获取多选框的id组合
 * 
 * @returns {String}
 */
function valueGetFromCheckbox() {
	var input = document.getElementsByTagName("input");
	var inputCount = input.length;
	var values = "";
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox" && input[i].checked
				&& input[i].className == 'arch') {
			if (values == "") {
				values += input[i].id;
			} else
				values += "," + input[i].id;
		}
	}
	return values;
}

/**
 * 获取多选框的id组合
 * 
 * @returns {String}
 */
function valueGetFromCheckbox2(className) {
	var input = document.getElementsByTagName("input");
	var inputCount = input.length;
	var values = "";
	for (var i = 0; i < inputCount; i++) {
		if (input[i].type == "checkbox" && input[i].checked
				&& input[i].className == className) {
			if (values == "") {
				values += input[i].id;
				values = values.replace(className, "");
			} else
				values += "," + input[i].id;
			values = values.replace(className, "");
		}
	}
	console.log(values);
	return values;
}

/**
 * 打标签
 */
function lableToArchives() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	$("#idValues").val(values);
	$("#queryForm")[0].submit();

}

function showcheckpass() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	document.getElementById('shadow').style.display = '';
	document.getElementById('checktype').style.display = '';
}
function returnback() {
	document.getElementById('checktype').style.display = 'none';
	document.getElementById('shadow').style.display = 'none';
}
function showbiaoqian() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	document.getElementById('shadow').style.display = '';
	document.getElementById('biaoqian').style.display = '';
}
function returnbiaoqian() {
	document.getElementById('biaoqian').style.display = 'none';
	document.getElementById('shadow').style.display = 'none';
}
function showqueren() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	document.getElementById('shadow').style.display = '';
	document.getElementById('queren').style.display = '';
}
function returnqueren() {
	document.getElementById('queren').style.display = 'none';
	document.getElementById('shadow').style.display = 'none';
}
/**
 * 档案审核
 */
function archCheck() {
	alert($("#checkboxthreeInput").val());
	return;
	var values = valueGetFromCheckbox();
	/*
	 * if (values == "") { alert("请选择档案。"); return; }
	 */
	// 给档案id串赋值
	$(".archivesIdStr-check").val(values);
	// 档案类型
	$(".check-type").val($(".type-select").val());
	// 审核结果
	$(".status-check").val(3);
	$.post("auditArchives", $("#checkForm").serialize(), function(
			responseResult) {
		alert(responseResult.responseParam.message);
		location.reload();
	});
}
/**
 * 档案删除
 */
function archDelete() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}
	$("#archivesIdStr").val(values);
	$.post("deleteArchives", $("#deleteForm").serialize(), function(
			responseResult) {
		alert(responseResult.responseParam.message);
		location.reload();
	});
}
/**
 * 档案公开
 */
function archOpen() {
	var values = valueGetFromCheckbox();
	if (values == "") {
		alert("请选择档案。");
		return;
	}

}